package com.bikes.mileagetracker.persistence

import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4
import com.bikes.mileagetracker.orm.BaseDataBaseHelper
import com.bikes.mileagetracker.entity.LocationEntity
import com.bikes.mileagetracker.service.PersistenceService
import com.bikes.mileagetracker.utils.TaskUtils
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class LocationEntityTest {

    val LOCATIONS = listOf<LocationEntity>(
            LocationEntity().apply {
                this.userId = "1"
                this.trackingId = "1"
                this.latitude = 37.422
                this.longitude = -122.084
            }, LocationEntity().apply {
        this.userId = "1"
        this.trackingId = "1"
        this.latitude = 37.422
        this.longitude = -122.084
    }, LocationEntity().apply {
        this.userId = "1"
        this.trackingId = "1"
        this.latitude = 37.422
        this.longitude = -122.084
    }, LocationEntity().apply {
        this.userId = "1"
        this.trackingId = "2"
        this.latitude = 37.422
        this.longitude = -122.084
    }, LocationEntity().apply {
        this.userId = "1"
        this.trackingId = "2"
        this.latitude = 37.422
        this.longitude = -122.084
    }, LocationEntity().apply {
        this.userId = "1"
        this.trackingId = "2"
        this.latitude = 37.422
        this.longitude = -122.084
    }, LocationEntity().apply {
        this.userId = "2"
        this.trackingId = "3"
        this.latitude = 37.422
        this.longitude = -122.084
    }, LocationEntity().apply {
        this.userId = "2"
        this.trackingId = "3"
        this.latitude = 37.422
        this.longitude = -122.084
    }, LocationEntity().apply {
        this.userId = "2"
        this.trackingId = "3"
        this.latitude = 37.422
        this.longitude = -122.084
    }
    )

    val LOCK = Object()

    fun notifyLock() {
        synchronized(LOCK) {
            LOCK.notify()
        }
    }

    fun lock() {
        synchronized(LOCK) {
            LOCK.wait(60000)
        }
    }

    @Test
    fun testLocationEntityOps() {

        val appContext = InstrumentationRegistry.getTargetContext()
        Assert.assertEquals("com.bikes.mileagetracker", appContext.packageName)

        BaseDataBaseHelper.createTable(BaseDataBaseHelper.getDatabase()!!, LocationEntity::class.java, true)

        val helper = object : BaseDataBaseHelper<LocationEntity>() {}

        while (PersistenceService.service == null) {
            PersistenceService.bind(appContext)
            Thread.sleep(100)
        }

        val service = PersistenceService.service!!


        LOCATIONS.forEach { entity ->
            TaskUtils.runInBackground {
                //service.saveUser(LOCATIONS[0])
                service.saveLocations(entity)
                notifyLock()
            }
            lock()

            var result = helper.byId(entity.id)
            Assert.assertNotNull(result)
            Assert.assertNotNull(result!!.id)
        }

        Assert.assertEquals(service.loadUserLocations("1").size, 6)

    }

}